"use strict";

/**
 * @ngdoc function
 * @name jgappApp.controller:TaskreviewCtrl
 * @description
 * # TaskreviewCtrl
 * Controller of the jgappApp
 */
angular
    .module("jgappApp")
    .controller(
        "CheckDeductController",
        function ($scope, $timeout, $routeParams, localStorageService, $location, $rootScope, FileService, TaskReviewService, DataService, Util, openApi, submitAudit, OpenFile) {
            // 详情接口，审批记录接口，提交审核接口
            var id = ($scope.process_id = $routeParams.id);
            $scope.taskId = id;

            $scope.details = []
            // 审批操作
            var aduitAction;
            var detailDataUrl;
            var TVtype;
            TVtype = Util.getTVType();
            switch (TVtype) {
                case Util.CHECK: //待审批
                    detailDataUrl = openApi.allDetailApi;
                    break;
                case Util.VIEW: //待审批
                    detailDataUrl = openApi.allDetailApi;
                    break;
                case Util.READ: //无需审批
                    detailDataUrl = openApi.getTaskInfo;
                    break;
                case Util.AUDIT: //审批
                case Util.APPEND: //审批
                case Util.SYNCHANDLE: //审批
                    detailDataUrl = openApi.getTaskInfo;
                    break;
                case Util.SYNCAUDIT: //并行审批
                    detailDataUrl = openApi.getTaskInfo;
                    break;
                case Util.CAUDIT: //自定义流程审批
                    detailDataUrl = openApi.getTaskInfo;
                    break;
                default:
            }
            // 查看附件
            $scope.goDetail = function (fileData) {
                if (Util.isImg(fileData)) {
                    FileService.showfile(fileData);
                } else if (Util.isFile(fileData)) {
                    OpenFile.open(fileData);
                } else {
                    Util.Toast("移动端不支持预览");
                }
            };
            // 新增抄送功能
            $scope.copyto = function () {
                Util.setRelMoNum(1);
                localStorageService.set("chooseUsrs", $scope.aduitUsers);
                localStorageService.set("aduitAction", aduitAction);
                $location.path("chooseuser/index/getFollows");
                $rootScope.$emit("closemasker");
            }
            $scope.auditText = "";
            $scope.check = function (item) {
                if (item.name === "不同意") {
                    $scope.hidedisagree = !$scope.hidedisagree
                } else {
                    $scope.showAuditContent(item)
                }
            };

            $scope.checkmore = function () {
                $scope.hidedisagree = !$scope.hidedisagree
            };

            $scope.showAuditContent = function (item, $event) {
                if (item.id === 20) {
                    // 如果是转交则执行这里的操作
                    Util.showFlowTransfer({
                      scope: $scope
                    })
                    $rootScope.$emit("showmasker");
                    return
                  }
                  if (item.id === 30) {
                    // 回退的操作
                    Util.showFallbackFlow({
                      scope: $scope,
                    });
                    $rootScope.$emit("showmasker");
                    return;
                  }
                $scope.hidedisagree = true
                $event && $event.stopPropagation()        // 设置蛇皮操作，当页面跳转时候保存起来
                aduitAction = item;
                $rootScope.btnTitle = item.name;//不显示默认值
                $scope.operationId = item.id;
                // 显示审批内容
                Util.showAuditContent({
                    scope: $scope
                });
                $scope.showDailog = true;
                $rootScope.$emit("showmasker");
            };
            $scope.$on('closemasker', function () {
                $scope.cancel();
                $rootScope.contenttemplate.remove();
            });
            $scope.confirm = function () {
                $scope.showDailog = false;
                $rootScope.$emit("closemasker");
                completeAudit();
            };
            $scope.cancel = function () {
                console.log("cancel");
                $scope.showDailog = false;
                $rootScope.$emit("closemasker");
                $rootScope.contenttemplate.remove();
            };

            var completeAudit = function () {
                var param = {};
                param.taskId = $scope.TaskReview.result.taskId;
                param.operationId = $scope.operationId;
                param.attachmentDesc = $scope.auditText;
                var details = $scope.TaskReview.result.details.concat($scope.details)
                param.deducts = angular.toJson(details);
                param.pkId = $scope.pkId;
                if ($rootScope.btnTitle === "不同意") {
                    param.notPass = 1;
                }
                if ($rootScope.btnTitle === "已阅") {
                    param.read = 1;
                }
                // 抄送人
                if ($scope.aduitUsers) {
                    param.userIds = $scope.aduitUsers.map(function (item) {
                        return item.id
                    });
                }
                // 审批附件
                param.attachmentName = $scope.attachmentName || "";
                // 快递单号
                if ($scope.expressNo) {
                    param.expressNo = $scope.expressNo;
                }
                param.url = openApi.appendDeductMoney;
                new submitAudit(param);
            };
            // 获取回显的详细信息
            var getDetailData = function (callback) {
                var options = {};
                options.param = {};
                options.param.processInstanceId = id;
                options.param.taskId = id;
                options.url = detailDataUrl;
                options.callback = callback;
                var TaskReview = new TaskReviewService(options);
                $scope.TaskReview = TaskReview;
            };
            // 该流程无需审批，设置查看回执
            var setDetailSeen = function (processInstanceId) {
                var param = {};
                param.noloader = true;
                param.processInstanceId = processInstanceId;
                DataService.getData(openApi.publicDoc, param).then(function (data) { });
            };
            var settaskRemind = function (taskData) {
                var Userdata, checked = false;
                Userdata = NativeUtil.getUserInfo();
                checked = taskData.processStatus == "审批中" &&
                    Userdata && Userdata.userId == taskData.userId;
                if (checked) {
                    Util.showRemindBtn({
                        scope: $scope
                    });
                }
                return ($scope.showTaskRemind = checked);
            };
            $scope.costTypeList = [
                {
                    key: "保证金",
                    value: 3
                },
                {
                    key: "押金",
                    value: 4
                },
                {
                    key: "缴纳",
                    value: 5
                },
                {
                    key: "成本",
                    value: 6
                },
                {
                    key: "收入",
                    value: 7
                }
            ];
            $scope.costTypechange = function () {
                $scope.picker.deductName = "";
                $scope.getcostsubTypeList();
            };
            $scope.getcostsubTypeList = function () {
                var param = {};
                param.type = "项目款项小类";
                param.parent = $scope.costTypeList.filter(function (item) {
                    return item.value == $scope.picker.deductType;
                })[0].key;
                Util.getKeyValue(param, function (data) {
                    $scope.costsubTypeList = data.data;
                });
            };
            $scope.showCertificate = function () {
                $scope.showCerPicker = true;
                $rootScope.$emit("showmasker");
            };
            $scope.deletedItem = function (index) {
                $scope.details.splice(index, 1);
                $scope.costindex = -1;
            };

            $scope.addCertificate = function () {
                $scope.picker = $scope.picker || {};
                var check = false;
                check = checkPicker($scope.picker);
                if (!check) {
                    return;
                }
                $scope.details.push($scope.picker);
                // 隐藏表单
                closePicker();
            };
            var closePicker = function () {
                $scope.picker = {};
                $rootScope.$emit("closemasker");
                $scope.showCerPicker = false;
            };
            $scope.$on("closemasker", function () {
                closePicker();
            });
            $scope.closePicker = function () {
                closePicker();
            };
            $scope.swipe = function (index) {
                $scope.costindex = index;
            };
            $scope.resetlist = function () {
                $scope.costindex = -1;
            };
            // 验证是否输入完整
            var checkPicker = function (data) {
                if (!data.deductAmount) {
                    Util.Toast("请填写扣款金额");
                    return;
                }
                if (!data.deductType) {
                    Util.Toast("请选择款项类型");
                    return;
                }
                if (!data.deductRemark) {
                    Util.Toast("请填写扣款事由");
                    return;
                }
                return true;
            };
            // 如果是审批中并且当前登录用户是当前发起人，可以显示提醒一下
            var init = function () {
                $rootScope.nofooter = false
                switch (TVtype) {
                    case Util.CHECK:
                        getDetailData(function (data, formKey) {
                            // 设置formKey用来进入关联模块
                            $scope.pkId = data.pkId
                            $scope.formKey = formKey;
                            $scope.processStatus = data.processStatus
                            // 设置提醒审批
                            settaskRemind(data);
                            // 显示关联模块
                            $timeout(
                                function () {
                                    Util.insertRelList({
                                        scope: $scope
                                    });
                                },
                                500
                            );
                        });
                        break;
                    case Util.VIEW:
                        getDetailData(function (data, formKey) {
                            // 设置formKey用来进入关联模块
                            $scope.pkId = data.pkId
                            $scope.formKey = formKey;
                            $scope.processStatus = data.processStatus
                            // 设置提醒审批
                            settaskRemind(data);
                            $timeout(
                                function () {
                                    Util.insertRelList({
                                        scope: $scope
                                    });
                                },
                                500
                            );
                        });
                        break;
                    case Util.READ: //通知发文无需显示审批记录
                        getDetailData(function (data, formKey) {
                            // 设置formKey用来进入关联模块
                            $scope.pkId = data.pkId
                            $scope.formKey = formKey;
                            // 通知发文自动审批
                            if (formKey == "Chief_PublicDoc") {
                                setDetailSeen(data.processInstanceId);
                            }
                            $timeout(
                                function () {
                                    Util.insertAduitUsers({
                                        scope: $scope
                                    });
                                },
                                500
                            );
                        });
                        $rootScope.nofooter = true
                        break;
                    case Util.AUDIT:
                    case Util.SYNCHANDLE: //审批
                    case Util.SYNCAUDIT:
                    case Util.CAUDIT:
                    case Util.APPEND: //审批
                        $scope.showaudit = true;
                        $scope.hidedisagree = true
                        getDetailData(function (data, formKey) {
                            $scope.pkId = data.pkId
                            $scope.formKey = formKey;
                            // 根据返回数据显示按钮
                            Util.showAudit({
                                scope: $scope
                            });
                            var aduitAction = localStorageService.get("aduitAction");
                            if (aduitAction) {
                                // 用延时，是为了在原来页面被清除之后才初始化渲染
                                $timeout(function () {
                                    // 审批人
                                    $scope.aduitUsers = localStorageService.get("chooseUsrs");
                                    if ($scope.aduitUsers) {
                                        $scope.aduitUserNames = $scope.aduitUsers.map(function (item) {
                                            return item.name
                                        }).join(",");
                                    }
                                    localStorageService.remove("aduitAction");
                                    localStorageService.remove("chooseUsrs");
                                    $scope.check(aduitAction);
                                }, 200)
                            }
                            $timeout(
                                function () {
                                    Util.insertAduitUsers({
                                        scope: $scope
                                    });
                                    Util.insertRemarkList({
                                        scope: $scope
                                    });
                                },
                                500
                            );
                        });
                        break;
                }
            };
            // 初始化
            init();
        }
    );
